package com.changgou.seckill.listener;


import com.alibaba.fastjson.JSON;
import com.changgou.seckill.dao.SeckillGoodsMapper;
import com.changgou.seckill.dao.SeckillOrderMapper;
import com.changgou.seckill.pojo.SeckillGoods;
import com.changgou.seckill.pojo.SeckillOrder;
import com.changgou.seckill.pojo.SeckillStatus;
import entity.SystemConstants;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;


@RabbitListener(queues = "queue.seckillorder") //用来表示监听秒杀的队列,直接写死是pay微服务中的队列
@Component
public class PayOrderSeckillListener {


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SeckillOrderMapper seckillOrderMapper;

    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;

    @RabbitHandler  //表示用这个方法处理消息
    public void handlerMsg(String msg) {   //处理字符串的请求

        //2.接收消息(map对象的json字符串)
        Map<String, String> map = JSON.parseObject(msg, Map.class);

        if (map != null) {
            if (map.get("return_code").equals("SUCCESS")) {

                //获取用户名,生成二维码的时间就把用户名传进来
                String attach = map.get("attach");
                Map<String,String> attachMap = JSON.parseObject(attach, Map.class);

                attachMap.put("username","szitheima" );//todo:用户名先固定写死

                if (map.get("result_code").equals("SUCCESS")) {//支付成功



                    //3.从redis中获取用户预订单
                    SeckillOrder seckillOrder = (SeckillOrder) redisTemplate.boundHashOps(SystemConstants.SEC_KILL_ORDER_KEY).get(attachMap.get("username"));

                    seckillOrder.setStatus("1"); //修改状态变成已经支付

                    //返回通知结果中有时间
                    String time_end = map.get("time_end");
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                    try {
                        Date parse = sdf.parse(time_end);
                        seckillOrder.setPayTime(parse); //支付时间
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                    //返回结果中有微信交易订单号
                    String transaction_id = map.get("transaction_id");
                    seckillOrder.setTransactionId(transaction_id);


                    //4.从预订单中存储到MySQL中
                    seckillOrderMapper.insertSelective(seckillOrder);

                    //5.删除掉原来redis中的预订单
                    redisTemplate.boundHashOps(SystemConstants.SEC_KILL_ORDER_KEY).delete(attachMap.get("username"));

                    //删除掉用户的排队标识,这样用户支付完再买其他商品时才可以进来排队
                    redisTemplate.boundHashOps(SystemConstants.SECK_KILL_GOODS_COUNT_KEY).delete(attachMap.get("username"));



                } else {
                    //支付失败---->删除订单

                    //1.微信返回支付失败信息

                    //2.恢复库存
                        //抢单状态存到redis中,方便用户查找当前排队状态,取出
                    SeckillStatus seckillStatus = (SeckillStatus) redisTemplate.boundHashOps(SystemConstants.SEC_KILL_USER_STATUS_KEY).get(attachMap.get("username"));
                    SeckillGoods seckillGoods = (SeckillGoods) redisTemplate.boundHashOps(SystemConstants.SEC_KILL_GOODS_PREFIX + seckillStatus.getTime()).get(seckillStatus.getGoodsId());

                    if (seckillGoods == null){
                       seckillGoods = seckillGoodsMapper.selectByPrimaryKey(seckillStatus.getGoodsId());
                    }

                    seckillGoods.setStockCount(seckillGoods.getStockCount()+1); //库存加1
                    //再次把加了库存的数据存到redis中去抢单
                    redisTemplate.boundHashOps(SystemConstants.SEC_KILL_GOODS_PREFIX + seckillStatus.getTime()).put(seckillStatus.getGoodsId(),seckillGoods);


                    //3.删除预订单
                        //删除掉预订单
                    redisTemplate.boundHashOps(SystemConstants.SEC_KILL_ORDER_KEY).delete(attachMap.get("username"));
                        //删除掉排队标识
                    redisTemplate.boundHashOps(SystemConstants.SECK_KILL_GOODS_COUNT_KEY).delete(attachMap.get("username"));

                }
            }
        }


    }

   /* @RabbitHandler  //表示用这个方法处理消息
    public  void handlerMsg(Map msg){   //表示用来处理map的请求



    }*/
}
